package com.nulstudio.cloud.inventory.controller;

import com.github.pagehelper.PageInfo;
import com.nulstudio.cloud.common.util.NulResult;
import com.nulstudio.cloud.inventory.domain.dto.TransferInventoryDto;
import com.nulstudio.cloud.inventory.domain.vo.InventoryVo;
import com.nulstudio.cloud.inventory.service.InventoryService;
import jakarta.annotation.Resource;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/inventory/warehouse/{warehouseId}/")
public class InventoryController {

    @Resource
    @NonNull
    private InventoryService inventoryService;

    @GetMapping
    @NonNull
    public NulResult<NulResult.PageInfoResult<InventoryVo>> all(
            @PathVariable int warehouseId,
            @RequestParam(name = "name", defaultValue = "") @NonNull String name,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "pageSize", defaultValue = "100000") int pageSize
    ) {
        final PageInfo<InventoryVo> pageInfo = inventoryService.fetchAll(warehouseId, name, page, pageSize);
        return NulResult.response(pageInfo.getList(), pageInfo.getTotal());
    }

    @PostMapping("/transfer")
    @NonNull
    public NulResult<Void> transfer(@PathVariable int warehouseId, @RequestBody @NonNull TransferInventoryDto transferInventoryDto) {
        inventoryService.transfer(warehouseId, transferInventoryDto);
        return NulResult.response();
    }

    @PostMapping("/item/{id}/transfer")
    @NonNull
    public NulResult<Void> transfer(@PathVariable int warehouseId, @PathVariable int id,
                                    @RequestParam int destination, @RequestParam BigDecimal quantity) {
        inventoryService.transfer(warehouseId, id, destination, quantity);
        return NulResult.response();
    }

    @PostMapping("/commodity/{commodityId}/supplier/{supplierId}")
    @NonNull
    public NulResult<Void> stock(@PathVariable int warehouseId, @PathVariable int commodityId,
                                 @PathVariable int supplierId,
                                 @RequestParam @NonNull BigDecimal quantity) {
        inventoryService.stock(warehouseId, commodityId, supplierId, quantity);
        return NulResult.response();
    }

    @DeleteMapping
    @NonNull
    public NulResult<Void> delete(@PathVariable int warehouseId, @RequestParam @NonNull List<Integer> id) {
        inventoryService.delete(warehouseId, id);
        return NulResult.response();
    }

    @DeleteMapping("/item/{id}")
    @NonNull
    public NulResult<Void> delete(@PathVariable int warehouseId, @PathVariable int id) {
        inventoryService.delete(warehouseId, id);
        return NulResult.response();
    }
}
